<?php 
	// TODO: debug
	// kalo baru pertamakali masuk site, terus update about me..
	// avatarnya ga langsung keganti, perlu refresh dolo...
	
	session_start();
	include_once("header.php");
	include_once("connect.php");
	include_once("user_model.php");
	include_once("post_model.php");
	include_once("do_listing.php");
	
	$achievement_model = new Achievement_Model();
	mysql_select_db("progin_171_13509047",$con);
	
	if(isset($_GET["id"])) {
		$profile_id = $_GET["id"];
		
		//cek ke database, apa ada user dgn Id tsb?
		$usm = new User_Model();
		$ret = $usm->getUserInfo($profile_id);
		
		if($ret == false) {
			echo "<div class='contentbox'>";
			echo "<p style='color:red' align='center'><b>No user with that ID</b></p>";
			echo "</div>";
			die();
		}
		
		$found = false;
		
		while($row = mysql_fetch_array($ret)) {
			global $username, $full_name, $email, $gender, $birth, $avatar, $about_me, $post_count, $comment_count;
			$username = $row["username"];
			$full_name = $row["full_name"];
			$email = $row["email"];
			$gender = $row["gender"];
			$birth = $row["birth"];
			$avatar = $row["avatar"];
			$about_me = $row["about_me"];
			$post_count = $row["post_count"];
			$comment_count = $row["comment_count"];
			$found = true;
		}
		
		if(!$found) {
			echo "<div class='contentbox'>";
			echo "<p style='color:red' align='center'><b>No user with that ID</b></p>";
			echo "</div>";
			die();
		}
	} else {
		header("Location: index.php");
	}

	
?>

<div id="bodycontent">
	<div class="contentcontainer">
		<div class="title-text">
		  <h1>Profile Information</h1>
		</div>		
		
		<div class="contentbox">
		<div id="profile_id">Username: <?php echo $username; ?></div>
		<div id="profile_fullname">Full name: <?php echo $full_name; ?></div>
		<div id="profile_avatar">Avatar: <img class="avatar" src="avatar/<?php echo $avatar; ?>" alt="avatar"/></div>
		<div id="profile_email">Email: <?php echo $email; ?></div>
		<div id="profile_gender">Gender: <?php echo $gender; ?></div>
		<div id="profile_about"> About Me: <?php echo $about_me; ?></div>
		<div id="profile_achieve">Achievement:
		<ul>
                <?php                   
                                        
                        $achieved_res = $achievement_model->getAllAchievementAchieved($profile_id);
                        $arr_achieved = array();
                        $i = 0;
                        while($achieved_row = mysql_fetch_array($achieved_res)) {
                                $arr_achieved[$i] = $achieved_row;
                                $i++;
                        }
                        
                        $all_achievement_res = $achievement_model->getAllAchievement();
                        
                        while($all_achievement_row = mysql_fetch_array($all_achievement_res)) {
                                echo $all_achievement_row["name"]." : ".$all_achievement_row["description"];
                                $achieved = false;
                                for($i=0;$i<count($arr_achieved);$i++) {
                                        if($arr_achieved[$i]["achievement_id"]==$all_achievement_row["ID"]) {
                                                echo "<span class='achievement-achieved-check'> achieved! </span>";
                                                $achieved = true;
                                                break;
                                        }
                                }
                                echo "<br />";
                        }               
                ?>      
        </ul>
		</div>
		<div id="profile_commentcount">Comment count: <?php echo $comment_count; ?></div>
		<div id="profile_contentcount">Content count: <?php echo $post_count; ?></div>
		
		<?php
			echo "<script type='text/javascript' src='profile.js'></script>";
			if(isset($_SESSION["userid"]) && $_SESSION["userid"] == $profile_id) {
				echo	"<h1>Edit profile</h1>";
				echo	"<form method='post' action='edit_profile.php' enctype='multipart/form-data'>";
				echo	"New Avatar: <input type='file' name='avatar' /> <br />";
				echo	"New Email: <input type='text' name='email' /> <br />";
				echo	"New About Me: <br/><textarea rets='5' cols='35' name='about'></textarea> <br />";
				echo	"<input type='submit' value='Done Editing' name='mit'/>";
				echo	"</form>";
			}
		?>
		
		</div>
	</div>
	
	<div class = "contentcontainer">
		<div class="title-text">
		  <h1>Content Posted</h1>
		</div>	
		<?php
			$pm = new Post_Model();
			$res = $pm->getUserPosts($profile_id);
			convertQuery($res, $profile_id, "id");
		?>
	</div>
	
</div>

<?php include_once("footer.php"); ?>


